using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using ChannelVN.Cached.Common;
using ChannelVN.Cached;

namespace ChannelVN.Cached.Config
{
    public class UrlRewriteRules : CollectionBase
    {
        private const string CACHE_NAME_FOR_URL_REWRITE_SETTING_FILE = "COMMON_CONFIGURATION_URL_REWRITE_SETTING";

        public static UrlRewriteRules GetCurrentRewriteRules()
        {
            object cacheData = CacheController.GetCacheDependencyInstance().Get(CACHE_NAME_FOR_URL_REWRITE_SETTING_FILE);
            if (null != cacheData)
            {
                try
                {
                    return (UrlRewriteRules)cacheData;
                }
                catch
                {
                }
            }
            try
            {
                string configFilePath = HttpContext.Current.Server.MapPath(AppConfigs.UrlRewriteRuleSettingFile); //@"D:\Running projects\VC Corporation\Dantri\Dantri.Cached\CacheSettings.config";
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configFilePath);

                UrlRewriteRules rules = new UrlRewriteRules();

                XmlNodeList nlstRules = xmlDoc.DocumentElement.SelectNodes("//rules/rule");

                for (int i = 0; i < nlstRules.Count; i++)
                {
                    UrlRewriteRule rule = new UrlRewriteRule();
                    rule.Name = nlstRules[i].Attributes["name"].Value;
                    rule.Url = nlstRules[i].SelectSingleNode("url").InnerText;
                    rule.Rewrite = nlstRules[i].SelectSingleNode("rewrite").InnerText;

                    rules.List.Add(rule);
                }

                XmlNode nodeFileSettingCacheExpire = xmlDoc.DocumentElement.SelectSingleNode("//Configuration/RewriteRulesFile");
                long fileSettingCacheExpire = Lib.Object2Long(nodeFileSettingCacheExpire.Attributes["cacheExpire"].Value);
                if (fileSettingCacheExpire <= 0)
                {
                    fileSettingCacheExpire = 3600;// default 1h
                }

                CacheController.GetCacheDependencyInstance().Add(configFilePath, CACHE_NAME_FOR_URL_REWRITE_SETTING_FILE, rules);
                return rules;
            }
            catch
            {
                return new UrlRewriteRules();
            }
        }

        public string GetMatchingRewrite(string url)
        {
            Regex rex;

            for (int i = 0; i < List.Count; i++)
            {
                UrlRewriteRule rule = (UrlRewriteRule)List[i];
                rex = new Regex(rule.Url, RegexOptions.IgnoreCase);
                Match match = rex.Match(url);

                if (match.Success)
                {
                    return rex.Replace(url, rule.Rewrite);
                }

            }

            return string.Empty;
        }

        public UrlRewriteRule GetMatchingRule(string url)
        {
            Regex rex;

            for (int i = 0; i < List.Count; i++)
            {
                UrlRewriteRule rule = (UrlRewriteRule)List[i];
                rex = new Regex(rule.Url, RegexOptions.IgnoreCase);
                Match match = rex.Match(url);

                if (match.Success)
                {
                    return rule;
                }

            }

            return new UrlRewriteRule();
        }

        public UrlRewriteRule this[int index]
        {
            get
            {
                return (UrlRewriteRule)List[index];
            }
        }

        public struct UrlRewriteRule
        {
            public UrlRewriteRule(string name, string url, string rewrite)
            {
                this.Name = name;
                this.Url = url;
                this.Rewrite = rewrite;
            }
            public string Name, Url, Rewrite;
        }
    }
}
